<?php
/**
 * 
 * SearchAction.class.php (前台搜索功能)
 *
 * @package      	jgsat
 * @author          liuxun QQ:147613338 <admin@jgsat.cn>
 * @copyright     	Copyright (c) 2008-2011  (http://www.jgsat.com)
 * @license         http://www.jgsat.com/license.txt
 * @version        	jgsat企业网站管理系统 v2.1 2011-03-01 jgsat.cn $
 */
if(!defined("JGsat")) exit("Access Denied");
class SearchAction extends BaseAction
{

	function _initialize()
    {	
		parent::_initialize();
    }

    public function index()
    {

        $url_s='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        $this->assign('url_s',$url_s);
        $catid =$_REQUEST['catid'];
        $where = " status=1 ";
        if($catid){
        if($_REQUEST['module']=='user' && $_REQUEST['catid']==4){

            $groupid=1;
            $where .=" and groupid= $groupid";
        }else{
        $where .=" and catid= $catid";}
        }
        //搜索
        $_REQUEST['id'] = $catid ;
        $p = max(intval($_REQUEST[C('VAR_PAGE')]), 1);
        $_REQUEST['keyword'] = $keyword = get_safe_replace($_REQUEST['keyword']);
        $_REQUEST['module'] = get_safe_replace($_REQUEST['module']);
        
        $this->assign($_REQUEST);
        $template = $_REQUEST['module'];

        $module = $_REQUEST['module'];



        // $template="index";
        if (APP_LANG) {
            $lang = LANG_NAME;
            $langid = LANG_ID;
            // $where .=" and lang= $langid";
            $this->assign('lang', $lang);
            $this->assign('langid', $langid);
        }
        $seo_title = "搜索";
        $this->assign('seo_title', $keyword );
        if ($keyword) {
            if (strstr($keyword, 'or')) {
                $keydo = ' or ';
                $keyword_arr = explode('or', $keyword);
            } elseif (strstr($keyword, ' ')) {
                $keydo = ' AND ';
                $keyword_arr = explode(' ', $keyword);
            }
            if (count($keyword_arr) > 1) {
                foreach ($keyword_arr as $key => $keywordz) {
                    $keyword_arr[$key] = ' title like "%' . trim($keywordz) . '%" ';
                }
                $where .= ' AND (' . implode($keydo, $keyword_arr) . ')';
            }elseif($module=='user'){
                $where .= ' AND (realname like "%' . $keyword . '%" ';
                $where .= ' or nickname like "%' . $keyword . '%") ';
            } else {
                $where .= ' AND title like "%' . $keyword . '%" ';
            }
        }
        $listRows = 12;

        $this->dao = M($module);
        //echo $where;exit;
        $count = $this->dao->where($where)->count();
        //	echo $this->dao->getLastSql();exit;
        $this->assign('jgcount', $count);
        if ($count) {
            import("@.ORG.Page");
            $page = new Page ($count, $listRows);
            $_REQUEST['p'] = '{$page}';
            $of=$listRows*($p-1);
           // $page->urlrule = URL('Home-Search/index', $_REQUEST);
            //$pages = $page->preshow();
            $field = $this->module[$cat['moduleid']]['listfields'];
            $field = '*';
            $list = $this->dao->field($field)->where($where)->order('id desc')->limit($of . ',' . $listRows)->select();
          //  echo $this->dao->getLastSql();exit;
            if ($_REQUEST['module'] == 'Store') {
                foreach ($list as $k => $r) {
                    $storepro = M("Product")->where("userid=" . $r["id"] . " and status=1")->order('id desc')->limit(4)->select();
                    $list[$k]["storeprolist"] = $storepro;
                }
            } elseif ($_REQUEST['module'] == 'User') {
                foreach ($list as $k => $r) {
                    $storepro = M("Share")->where("userid=" . $r["id"] . " and status=1")->order('id desc')->limit(4)->select();
                    $list[$k]["usersharelist"] = $storepro;
                    $list[$k]["countusershare"] = count($storepro);
                }
            }
            $this->assign('pages', $pages);
            if($module=='Zforum_theme') {
                foreach ($list as &$v) {
                    $plateid = $v['plateid'];
                    if ($plateid) {
                        $plate = M('Zforum_plate')->where('id=' . $plateid)->find();
                        $v['platename'] = $plate['name'];
                        // $this->assign ('plate',$plate);
                    }
                }
            }

            $this->assign('list', $list);
        }
      // print_r($list);exit;
        $this->assign('search_m',$module);
        $this->assign('search_catid',$catid);
        $this->assign('searchid',$_REQUEST['searchid']);

         if($template=='case' && $catid=='2' ){
             $template='case2';

         }
       // $plateid = $list['plateid'];


//print_r($list);exit;
        if($p>1){
                    if($module=='user') {
                        foreach ($list as &$v) {
                            $arr = M('Case')->field('id,pic')->where("userid=" . $v['id'] . " and status=1")->limit('2')->select();
                            $v['Case'] = $arr;
                            //擅长空间
                            $itemType = explode(",", $v['itemType']);
                            //  print_r($itemType);exit;
                            $where = "genre=17";
                            if ($itemType[0]) {
                                $where .= " and id=" . $itemType[0];
                            }
                            $arr = M('Label')->where($where)->find();
                            $v['itemType'] = $arr['title'];
                            //价格
                            $where_c = "keyid=34";
                            if ($v['charge']) {
                                $where_c .= " and typeid=" . $v['charge'];
                            }
                            $charge = M('Type')->where($where_c)->find();
                            $v['charge'] = $charge['name'];
                            //所在地
                            $v['p'] = jgarea($v['province']);
                            $v['c'] = jgarea($v['city']);
                        }
                        $url_s = unsetParam('p', $url_s);
                        $n = $p + 1;
                        $url = $url_s . '&p=' . $n;
                        $data = array('list' => $list, 'p' => $url);
                        // print_r($data);exit;
                        echo json_encode($data);//转换格式
                    }
                    if($module=='case'){
                        foreach($list as &$v){
                            $user = M('User')->where('id='.$v['userid'])->find();
                            //name="Label" where="genre=17" id="v" limit="50"itemType
                            $itemType = M('Label')->where('id='.$v['itemType'])->find();
                            $style = M('Label')->where('id='.$v['style'])->find();
                            $v['itemType']=$itemType['title'];
                            $v['style']=$style['title'];
                            $v['avatar']=$user['avatar'];
                            if($catid==1){
                                $v['date']=date("m月d日 H:i:s",$v['createtime']);
                            }else{
                                $v['date']=date("m月d日",$v['createtime']);
                            }
                        }
                        $url_s=unsetParam('p', $url_s);
                        $n=$p+1;
                        $url=$url_s.'&p='.$n;
                        $data = array('list' => $list, 'p' => $url);
                        // print_r($data);exit;
                        echo json_encode($data );//转换格式
                    }
                    if($module=='Product'){
                        foreach($list as &$v){
                            $brand['id']='';
                            $brand['title']='';
                            $brand['pic_mix']='';
                            if($v['brandid']) {
                                $brand = M('Brand')->where('id=' . $v['brandid'])->find();
                            }
                            $v['brandid']=$brand['id'];
                            $v['brand_t']=$brand['title'];
                            $v['pic_mix']=$brand['pic_mix'];
                            $v['title']=str_cut($v['title'],10);
                            //$v['date']=date("m-d ",$v['createtime']);
                        }
                        $url_s=unsetParam('p', $url_s);
                        $n=$p+1;
                        $url=$url_s.'&p='.$n;
                        $data = array('list' => $list, 'p' => $url);
                        //print_r($data);exit;
                        echo json_encode($data );//转换格式
                    }

            if($module=='Zforum_theme'){
                foreach($list as &$v){
                    $v['date']=date("m月d日 H:i:s",$v['createtime']);
                    $v['update']='';
                    if($v['updatetime']) {
                        $v['update'] = date("m月d日 H:i:s", $v['updatetime']);
                    }
                }
                $url_s=unsetParam('p', $url_s);
                $n=$p+1;
                $url=$url_s.'&p='.$n;
                $data = array('list' => $list,'plate'=>$plate ,'p' => $url);
                // print_r($data);exit;
                echo json_encode($data );//转换格式
            }

        }else{
            $this->display('Search:' . $template);}

    } 
}
?>